//
//  SwiftUIState.swift
//  SwiftUICode
//
//  Created by yangkl on 2024/2/19.
//

import SwiftUI

struct SwiftUIState: View {
    
    @State
    private var isPlaying = false
    
    @State
    private var counter = 0
    
    var body: some View {
        Button{
            isPlaying.toggle()
        }label: {
            Image(systemName: isPlaying ? "stop.circle.fill":"play.circle.fill")
                .font(.system(size: 150))
                .foregroundColor(isPlaying ? .red : .green)
        }
        
        CounterButton(counter: $counter, color: .red)
        CounterButton(counter: $counter, color: .blue)
        CounterButton(counter: $counter, color: .green)
    }
}

struct CounterButton: View {
    
    @Binding var counter: Int
    
    var color: Color
    
    var body: some View {
        Button{
            counter+=1
        }label: {
            Circle()
                .frame(width: 120, height: 120)
                .foregroundColor(color)
                .overlay{
                    Text("\(counter)")
                        .font(.system(size: 80, weight: .bold, design: .rounded))
                        .foregroundColor(.white)
                }
        }
    }
}

struct Homework: View{
    
    @State
    private var sum: Int = 0
    @State
    private var num1: Int = 0
    @State
    private var num2: Int = 0
    @State
    private var num3: Int = 0
    
    var body: some View{
        Text("\(num1+num2+num3)")
            .font(.system(size: 100, weight: .bold, design: .rounded))
        
        HStack{
            CounterButton(counter: $num1, color: .red)
            CounterButton(counter: $num2, color: .blue)
            CounterButton(counter: $num3, color: .green)
        }
    }
}

#Preview {
    SwiftUIState()
}

#Preview {
    Homework()
}
